package com.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yiyang.constant.ResultCode;
import com.yiyang.domain.Result;
import com.yiyang.domain.po.NurseContent;
import com.yiyang.domain.po.NurseLevel;
import com.yiyang.domain.po.NurseLevelItem;
import com.yiyang.exception.BusinessException;
import com.yiyang.service.NurseContentService;
import com.yiyang.service.NurseLevelItemService;
import com.yiyang.service.NurseLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nurselevel")
public class NurseLevelController {

    @Autowired
    private NurseLevelService nurseLevelService;

    @GetMapping("/listNurseLevel")
    public Result<List<NurseLevel>> listNurseLevel(Integer levelStatus){
        LambdaQueryWrapper<NurseLevel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(levelStatus !=null,NurseLevel::getLevelStatus,levelStatus);

        List<NurseLevel> list = nurseLevelService.list(lqw);

        return Result.success(list);
    }

    @PostMapping("/addNurseLevel")
    public Result addNurseLevel(@RequestBody NurseLevel nurseLevel){
        boolean flag = nurseLevelService.save(nurseLevel);
        if(flag){
            return Result.success(flag);
        }else{
            return Result.fail(ResultCode.NURSELEVEL_ADD,"新增护理级别失败！");
        }
    }

    @PutMapping("/updateNurseLevel")
    public Result updateNurseLevel(@RequestBody NurseLevel nurseLevel){
        nurseLevel.setLevelName(null);
        nurseLevel.setIsDelete(null);

        boolean flag = nurseLevelService.updateById(nurseLevel);
        if(flag){
            return Result.success(flag);
        }else{
            return Result.fail(ResultCode.NURSELEVEL_UPDATE,"修改护理级别失败！");
        }
    }

    @Autowired
    private NurseLevelItemService nurseLevelItemService;

    @PostMapping("/addItemToLevel")
    public Result addItemToLevel(@RequestBody NurseLevelItem nurseLevelItem){
        boolean flag = nurseLevelItemService.save(nurseLevelItem);

        if(flag){
            return Result.success();
        }else{
            return Result.fail(ResultCode.NURSELEVELITEM_ADD,"维护护理项目到护理级别中失败");
        }
    }

    @Autowired
    private NurseContentService nurseContentService;

    @GetMapping("/listNurseItemByLevel")
    public Result<List<NurseContent>> listNurseItemByLevel(Integer levelId){
        List<NurseContent> nurseContentList = nurseContentService.listNurseItemByLevel(levelId);
        return Result.success(nurseContentList);
    }


    @DeleteMapping("/removeNurseLevelItem")
    public Result removeNurseLevelItem(Integer levelId,Integer itemId){

        if(levelId ==null || itemId == null){
            throw new BusinessException(ResultCode.NURSELEVELITEM_REMOVE,"没有传递级别id或者项目id,不能移除");
        }

        LambdaQueryWrapper<NurseLevelItem> lqw = new LambdaQueryWrapper<>();
        lqw.eq(NurseLevelItem::getLevelId,levelId)
                .eq(NurseLevelItem::getItemId,itemId);

        boolean flag = nurseLevelItemService.remove(lqw);

        if(flag){
            return Result.success();
        }else{
            return Result.fail(ResultCode.NURSELEVELITEM_DELETE,"移除护理级别中的护理项目失败");
        }
    }
}
